package com.anjiplus.template.gaea.business.work.data.controller;

import com.anji.plus.gaea.bean.ResponseBean;
import com.anji.plus.gaea.curd.controller.BaseResponse;
import com.anjiplus.template.gaea.business.work.data.entity.Data;
import com.anjiplus.template.gaea.business.work.data.service.DataService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 数据controller
 *
 * @author yxm
 */
@RestController
@RequestMapping("/data")
public class DataController extends BaseResponse {

    private DataService dataService;

    public DataController(DataService dataService) {
        this.dataService = dataService;
    }

    /**
     * 查询全部参数点数据
     *
     * @return
     */
    @GetMapping("/listAll")
    public ResponseBean listAll() {
        Map<String, Object> result = new HashMap<>();
        dataService.listAll().stream().forEach(item -> result.put(item.getPoint().getCode(), item.getValue()));
        return responseSuccessWithData(result);
    }

    /**
     * 查询全部参数点数据
     *
     * @return
     */
    @GetMapping("/listAll2")
    public List<Map<String, Object>> listAll2() {
        List<Data> dataList = dataService.listAll().stream().filter(item->item.getPoint().getName().equals("瞬时流量")).collect(Collectors.toList());
        List<Map<String, Object>> result = new ArrayList<>();
        for (int i = 0; i < dataList.size(); i++) {
            Map<String, Object> data = new HashMap<>();
            data.put("code",dataList.get(i).getPoint().getCode());
            data.put("name",dataList.get(i).getPoint().getName() + i);
            data.put("value",Double.parseDouble(dataList.get(i).getValue()));
            result.add(data);
        }
        return result;
    }

    /**
     * 根据站点id查询参数点数据
     *
     * @param id
     * @return
     */
    @GetMapping("/listByStationId/{id}")
    public ResponseBean listByStationId(@PathVariable String id) {
        Map<String, Object> result = new HashMap<>();
        dataService.listByStationId(id).stream().forEach(item -> result.put(item.getPoint().getCode(), item.getValue()));
        return responseSuccessWithData(result);
    }

    /**
     * 根据参数点id查询某参数点数据
     *
     * @param id
     * @return
     */
    @GetMapping("/getByPointId/{id}")
    public ResponseBean getByPointId(@PathVariable String id) {
        Map<String, Object> result = new HashMap();
        String[] ids = {id};
        Data data;
        try {
            data = dataService.listByPointIds(ids).get(0);
        } catch (IndexOutOfBoundsException e) {
            return failure();
        }
        result.put(data.getPoint().getCode(), data.getValue());
        return responseSuccessWithData(result);
    }

    /**
     * 根据参数点id列表查询数据列表
     *
     * @param idStr
     * @return
     */
    @GetMapping("/listByPointIds/{idStr}")
    public ResponseBean listByPointIds(@PathVariable String idStr) {
        String[] ids = idStr.split(",");
        Map<String, Object> result = new HashMap<>();
        List<Data> dataList = dataService.listByPointIds(ids);
        dataList.forEach(item -> result.put(item.getPoint().getCode(), item.getValue()));
        return responseSuccessWithData(result);
    }

    /**
     * 根据参数点code查询某参数点数据
     *
     * @param code
     * @return
     */
    @GetMapping("/getByPointCode/{code}")
    public ResponseBean getByPointCode(@PathVariable String code) {
        Map<String, Object> result = new HashMap<>();
        String[] codes = {code};
        Data data;
        try {
            data = dataService.listByPointCodes(codes).get(0);
        } catch (IndexOutOfBoundsException e) {
            return failure();
        }
        result.put(data.getPoint().getCode(), data.getValue());
        return responseSuccessWithData(result);
    }

    /**
     * 根据参数点code列表查询数据列表
     *
     * @param codeStr
     * @return
     */
    @GetMapping("/listByPointCodes/{codeStr}")
    public ResponseBean listByPointCodes(@PathVariable String codeStr) {
        String[] codes = codeStr.split(",");
        Map<String, Object> result = new HashMap<>();
        List<Data> dataList = dataService.listByPointCodes(codes);
        dataList.forEach(item -> result.put(item.getPoint().getCode(), item.getValue()));
        return responseSuccessWithData(result);
    }

}
